package com.zczy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zczy.entity.ZczyItem;
import com.zczy.entity.ZczyItemInfo;
import com.zczy.service.ZczyItemInfoService;
import com.zczy.service.ZczyItemService;
import com.zczy.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author xcl
 * @since 2021-07-04
 */
@Controller
@RequestMapping("/zczyItem")
public class ZczyItemController {

    @Autowired
    private ZczyItemService zczyItemService;

    @Autowired
    private ZczyItemInfoService zczyItemInfoService;


    //交易类项目
    @GetMapping("transactionalOne/{id}")
    public String Transactional(@PathVariable("id")Integer id, Model model){
        ZczyItem byId = zczyItemService.getById(id);

        model.addAttribute("byId",byId);
        BigDecimal targetAmount = byId.getTargetAmount();//目标金额
        BigDecimal perMoney = byId.getPerMoney();//起投金额
        BigDecimal actAmount = byId.getActAmount();//已投金额
        //subtract减法；divide除法
        BigDecimal m =targetAmount.subtract(actAmount);
        BigDecimal divide = m.divide(perMoney);
        double v = divide.doubleValue();//Big Decimal转Double
        double result = Math.floor(v);//需要向下取整
        int r = (int) result;//取整
        model.addAttribute("result",r);

        //求百分比
        double v2 = actAmount.doubleValue();
        double v3 = targetAmount.doubleValue();
        double v4 = v2 / v3;//已投金额除于目标金额
        double d = v4 * 100;
        int floor = (int)d;
        model.addAttribute("floor",floor);
        String proportion = "width:" + floor + "%";
        model.addAttribute("proportion",proportion);

        return "交易类项目";
    }

    //展示类详情
    @GetMapping("/zczyItemOne/{id}")
    public String SelectItemType(@PathVariable("id") Integer id, Model model){
        ZczyItem zczyItem = zczyItemService.getById(id);

        model.addAttribute("zczyItem",zczyItem);

        String beginTime = zczyItem.getBeginTime();
        String endTime = zczyItem.getEndTime();
        long dayTime = DateUtil.parseDay(endTime, beginTime);//通过工具类计算剩余天数
        model.addAttribute("dayTime",dayTime);

        //计算剩余份数
        //目标金额-已投金额/起投金额
        BigDecimal targetAmount = zczyItem.getTargetAmount();//目标金额
        BigDecimal perMoney = zczyItem.getPerMoney();//起投金额
        BigDecimal actAmount = zczyItem.getActAmount();//已投金额
        //subtract减法；divide除法
        BigDecimal m =targetAmount.subtract(actAmount);
        BigDecimal divide = m.divide(perMoney);
        double v = divide.doubleValue();//Big Decimal转Double
        double result = Math.floor(v);//需要向下取整
        int r = (int) result;//取整
        model.addAttribute("result",r);

        //求百分比
        double v2 = actAmount.doubleValue();
        double v3 = targetAmount.doubleValue();
        double v4 = v2 / v3;//已投金额除于目标金额
        double d = v4 * 100;
        int floor = (int)d;
        model.addAttribute("floor",floor);
        String proportion = "width:" + floor + "%";
        model.addAttribute("proportion",proportion);

        //资质审核
        QueryWrapper<ZczyItemInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", id);
        ZczyItemInfo one = zczyItemInfoService.getOne(wrapper);
        System.out.println(one.getLegalIdCard());
        model.addAttribute("one",one);

        return "展示类详情";


    }

}

